// pages/plane/index.js
const app = getApp()
var timer = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    top: 0,
    isShow: false,
    // animation: '',
    showGuize: false,
    token: '',
    renci:0,
    showFont: '授权后即可参与抢红包、夺金币',
    skin: 1,
    bg_url: '//resource.160nurse.com/canvas/healthy/img/plane/bg_' + wx.getStorageSync('skinId') + '.png',
    title_url: '//resource.160nurse.com/canvas/healthy/img/plane/title.png',
    openSkin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.hasOwnProperty('link')) {
      wx.redirectTo({
        url: 'index',
      })
    }
    if (wx.getStorageSync('skinId') == '') {
      wx.setStorageSync('skinId', 1)
    }
    console.log(app.skinId)
    app.updateFlag(this)
    let that = this
    let unit = 0
    wx.getSystemInfo({
      success: function(res) {
        unit: res.screenHeight/100
        timer = setInterval(() => {
          if (that.data.top < 100) {
            let top = that.data.top
            top += 0.02
            that.setData({
              top: top,
              skin: wx.getStorageSync('skinId'),
              bg_url: '//resource.160nurse.com/canvas/healthy/img/plane/bg_' + wx.getStorageSync('skinId') +'.png'
            })
          } else {
            that.setData({
              top: 0,
              skin: wx.getStorageSync('skinId'),
              bg_url: '//resource.160nurse.com/canvas/healthy/img/plane/bg_' + wx.getStorageSync('skinId') + '.png'
            })
          }
        }, 10)
      },
    })
    this.getToken()
    this.getRenci()
  },
  getToken() {
    wx.showLoading({
      title: '资源获取中',
    })
    let that = this
    app.request(app.apiDomain + 'index.php?c=games&a=get_game_token',{},function (res){
      wx.hideLoading()
      if (res.data.status == 1) {
        wx.setStorageSync('token', res.data.data)
      }
    }, 'POST')
  },
  getRenci () {
    let that = this
    app.request(app.apiDomain + 'index.php?c=games&a=get_plane_stat', {}, function (res) {
      wx.hideLoading()
      if (res.data.status == 1) {
        that.setData({
          renci: res.data.data.num || 0
        })
      }
    }, 'POST')
  },
  start () {
    let that = this
    if (!app.isAuth) {
      that.setData({
        isShow: true
      })
    } else {
      wx.reLaunch({
        url: 'detail',
      })
    }
  },
  myred () {
    let that = this
    if (!app.isAuth) {
      that.setData({
        isShow: true
      })
    } else {
      wx.navigateTo({
        url: 'cash',
      })
    }
  },
  openSkin () {
    this.setData({
      openSkin: true
    })
  },
  closeSkin () {
    this.setData({
      openSkin: false
    })
  },
  // 切换皮肤
  toggleSkin (e) {
    let that = this
    let id = e.target.dataset.id
    wx.setStorageSync('skinId', id)
    // console.log(id)
    if (id != 3) {
      that.setData({
        skin: id,
        openSkin: false,
        bg_url: '//resource.160nurse.com/canvas/healthy/img/plane/bg_' + id + '.png',
        title_url: '//resource.160nurse.com/canvas/healthy/img/plane/title.png'
      })
      console.log(that.data.bg_url)
    } else {
      that.setData({
        skin: id,
        openSkin: false,
        bg_url: '//resource.160nurse.com/canvas/healthy/img/plane/bg_' + id + '.png',
        title_url: '//resource.160nurse.com/canvas/healthy/img/plane/title_'+ id +'.png'
      })
    }
    
  },
  openGuize() {
    // console.log(111)
    this.setData({
      showGuize: true
    })
  },
  closeGuize() {
    this.setData({
      showGuize: false
    })
  },
  authSuccess(e) {
    this.setData({
      isShow: false
    })
    // wx.navigateTo({
    //   url: 'rank',
    // })
  },
  authFail(e) {
    this.setData({
      isShow: false
    })
  },
  click() {
    this.setData({
      isShow: false,
      // isAuth: app.isAut
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    app.SystemInfo(this)
  },
  return_index () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  return_act (){
    let pages = getCurrentPages()
    if (pages.length == 1) {
      wx.redirectTo({
        url: '../index/index',
      })
    } else {
      wx.navigateBack({
        
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var nickName = '';
    var userId = 0;
    try {
      nickName = wx.getStorageSync('nickname');
      userId = wx.getStorageSync('user_id');
    } catch (e) { }
    return {
      path: "/pages/plane/index?user_id=" + userId,
      title: '打水果，拼手速，释放你的小宇宙！',
      imageUrl: app.resourceDomain + 'canvas/healthy/img/plane/comon.png'
    }
  }
})